<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>作品管理</el-breadcrumb-item>
      <el-breadcrumb-item>作品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>

      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入作品名搜索"
                    v-model="searchWorks"
                    clearable
                    @clear="getWorksList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getWorksList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddWorkPage">添加作品</el-button>
        </el-col>
      </el-row>
      <el-table :data="worksList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="作品名" prop="workName"></el-table-column>
        <el-table-column label="作品分类" prop="categoryId"></el-table-column>
        <el-table-column label="发布人" prop="username"></el-table-column>
        <el-table-column label="发布时间" prop="workDate">
          <template slot-scope="scope">
            {{ $moment(scope.row.workDate).format('YYYY-MM-DD') }}
          </template>
        </el-table-column>
        <el-table-column label="点赞数" prop="workLikesNums"></el-table-column>
        <el-table-column label="作品图" prop="workFilename">
          <template slot-scope="scope">

            <el-image
              v-if="scope.row.workFileType === 1"
              style="width: 30%;cursor: pointer;"
              :src="scope.row.workFilename"
              :preview-src-list="imgList">
            </el-image>

            <div v-else>
              <video :controls="true"
                     style="position: relative;width: 283px;height: 200px;left: -20px;"
                     :src="scope.row.workFilename">
              </video>
            </div>
          </template>

        </el-table-column>
        <el-table-column label="状态" prop="status">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :inactive-value=0
              :active-value=1
              @change="changeWorkStatus(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" style="display: inline-block;" width="170px">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="修改作品信息" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini"
                         @click="showEditDialog(scope.row.workId)"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除作品" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" @click="deleteWorks(scope.row.workId)"
                         size="mini"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

    </el-card>

    <el-dialog
      title="修改作品信息"
      :visible.sync="editDialogVisible"
      width="50%"
      :close-on-click-modal="false"
      @close="editDialogClosed">
      <el-form :model="editForm" ref="editFormRef" label-width="70px">

        <el-form-item label="作品名称" prop="workName">
          <el-input v-model="editForm.workName"></el-input>
        </el-form-item>
        <el-form-item label="作品分类" prop="categoryName">
          <el-cascader
            expand-trigger="hover"
            v-model="selectedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            clearable
            @change="handleChange"></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="changeWorks(editForm)">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
    import {changeWorkById, deleteWorkById, getWorkInfoById, searchWorksInfo} from "../../assets/api/worksInfoApi";
    import {getAllCategory} from "../../assets/api/categoryApi";

    export default {
        name: 'worksInfo',
        data() {
            return {
                worksList: [],
                searchWorks: '',
                pageNum: 1,
                pageSize: 5,
                total: 0,
                editDialogVisible: false,
                editForm: {},
                imgList: [],
                parentCateList: [],
                cascaderProps: {
                    value: 'categoryId',
                    label: 'categoryName',
                    children: 'children'
                },
                selectedKeys: [],
            }
        },
        methods: {
            getWorksList() {
                let data = {};
                data.workName = this.searchWorks;
                searchWorksInfo(this.pageNum, this.pageSize, data).then((res) => {
                    if (res.code === 200) {
                        this.worksList = res.data.records;
                        this.total = res.data.total;
                        for (let i = 0; i < this.worksList.length; i++) {
                            this.imgList[i] = this.worksList[i].workFilename;
                        }
                    }

                })
            },
            goAddWorkPage() {
                this.$router.push(`/addWork`)
            },
            handleSizeChange(newSize) {
                this.pageSize = newSize;
                this.getWorksList();
            },
            handleCurrentChange(newPage) {
                this.pageNum = newPage;
                this.getWorksList();
            },
            showEditDialog(workId) {
                this.getCategoryList();
                this.editDialogVisible = true;
                let data = {};
                data.workId = workId;
                getWorkInfoById(data).then((res) => {
                    this.editForm = res.data;
                })
            },
            getCategoryList() {
                let data = {};
                data.categoryLevel = 0;
                getAllCategory(this.pageNum, this.pageSize, data).then((res) => {
                    if (res.code !== 200) {
                        return this.$message.error(res.message);
                    }
                    this.parentCateList = res.data.records;
                })
            },
            editDialogClosed() {
                this.$refs.editFormRef.resetFields();
            },
            handleChange() {
                this.editForm.rootCategoryId = this.selectedKeys[0];
                this.editForm.categoryId = this.selectedKeys[1];
            },
            changeWorks() {
                this.$refs.editFormRef.validate(valid => {
                    if (!valid) return;
                    changeWorkById(this.editForm).then((res) => {
                        if (res.code !== 200) {
                            this.$message.error(res.message);
                        }
                        this.editDialogVisible = false;
                        this.getWorksList();
                        this.$message.success(res.message);
                    })
                })
            },
            changeWorkStatus(work) {
                changeWorkById(work).then((res) => {
                    if (res.code === 200) {
                        this.$message.success('更改状态成功');
                    } else {
                        this.$message.error('更改状态失败');
                    }
                })
            },
            deleteWorks(workId) {
                const confirmResult = this.$confirm(
                    '此操作将永久删除该作品, 是否继续?',
                    '提示',
                    {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                    let data = {};
                    data.workId = workId;
                    deleteWorkById(data).then((res) => {
                        if (res.code !== 200) {
                            return this.$message.error(res.message);
                        }
                        this.getWorksList();
                        this.$message.success(res.message);
                    })

                }).catch(() => {
                    this.$message.info('已取消删除');
                });

            },
        },
        created() {
            this.getWorksList();
        },
    }
</script>

<style scoped>
  @import "../../styles/css/global.css";

</style>
